package alarm

import (
	"iot-base/auth/api/logic"
	"iot-base/auth/logger"
	"iot-base/auth/types"
	"iot-base/auth/utils"
	"iot-base/common/eventtype"
	"iot-base/common/notify"
)

type Event struct {
}

func NewEvent() *Event {
	return &Event{}
}

func (e *Event) Run() {
	notify.SubMsg(false, "event", &eventtype.Event{}, func(t string, b interface{}) {
		param, ok := b.(*eventtype.Event)
		if ok {
			logger.Log.Infof("sub Event msg:=%v", param)
			utils.AntsPool.Submit(func() {
				e.sendEvent(*param)
			})
		}
	})
}

func (e *Event) sendEvent(param eventtype.Event) {
	req := types.RespEvent{
		Id:          param.UID,
		ThingId:     param.TID,
		EventID:     param.EventID,
		Time:        types.JsonTime(param.Time),
		Level:       param.Level,
		StationName: param.StationName,
		EventName:   param.EventName,
		StationID:   param.StationID,
	}
	if param.Params == nil {
		req.Params = map[string]interface{}{}
	} else {
		req.Params = param.Params
	}
	logic.NewAlarmLogic().SendEventMsg(req)
}
